package com.houfukude.Activity;

import java.util.ArrayList;
import java.util.List;
import com.houfukude.NoteList.NoteListAdapter;
import com.houfukude.NoteList.ItemValue;
import com.houfukude.Utils.ActionBar_Utils;
import com.houfukude.xtestmgr.R;

import Utils.V;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class NoteListActivity extends ActionBarActivity {
	private String DEBUG_TAG = "NoteListActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		List<ItemValue> list = new ArrayList<ItemValue>();
		setContentView(R.layout.fragment_note_list);
		ListView lv = (ListView) findViewById(R.id.note_list);
		
		SQLiteDatabase db = openOrCreateDatabase("test.db",
				Context.MODE_PRIVATE, null);
		Cursor c = db.rawQuery("SELECT * FROM ItemValue", null);
		while (c.moveToNext()) {
			ItemValue item_value = new ItemValue();
			item_value.setDay(c.getString(c.getColumnIndex("day")));
			item_value.setTime(c.getString(c.getColumnIndex("time")));
			item_value.setTitle(c.getString(c.getColumnIndex("title")));
			item_value.setDescription(c.getString(c.getColumnIndex("description")));
			item_value.setLink(c.getString(c.getColumnIndex("link")));
			item_value.setExt1(c.getInt(c.getColumnIndex("ext1")));
			item_value.setExt2(c.getInt(c.getColumnIndex("ext2")));
			item_value.setExt3(c.getInt(c.getColumnIndex("ext3")));
			list.add(item_value);
		}
		c.close();
		db.close(); 
//		for (int i = 0; i < 100; i++) {
//			ItemValue item_value = new ItemValue();
//			item_value.setDay("04月15日");
//			item_value.setTime("14:0" + i);
//			item_value.setTitle("记录" + i);
//			item_value.setDescription("test" + i);
//			item_value.setLink("ZK1 第" + i + "层");
//			item_value.setExt1(i % 2);
//			item_value.setExt2(i % 3);
//			item_value.setExt3(i % 5);
//			list.add(item_value);
//		}
		NoteListAdapter notelistAdapter = new NoteListAdapter(list, this);

		lv.setAdapter(notelistAdapter);

		OnItemClickListener listener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ItemValue value = (ItemValue) view.getTag();
				Toast.makeText(
						getApplicationContext(),
						"position:" + position + " title :"
								+ value.getTitle(),
						Toast.LENGTH_SHORT).show();
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), NewNoteActivity.class);
				startActivityForResult(intent, 0);
			}
		};
		lv.setOnItemClickListener(listener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.note_list, menu);
		ActionBar_Utils.restoreActionBar(this, getSupportActionBar());
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.d(DEBUG_TAG, "ItemSelected item.getItemId():" + item.getItemId());
		switch (item.getItemId()) {
		/**
		 * note_list.xml
		 */
		case R.id.action_add_note:
			ActionBar_Utils.AddNoteItemSelected(item, this);
			break;
		case R.id.action_search:
			ActionBar_Utils.SearchNoteItemSelected(item, this);
			break;
		case R.id.action_filter:
			ActionBar_Utils.NoteFilterItemSelected(item, this);
			break;
		case R.id.action_settings:
			Toast.makeText(this, "settings for " + getTitle(),
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.action_refresh:

			ActionBar_Utils.RefreshItemSelected(item, this);
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// super.onActivityResult(arg0, arg1, arg2);
		Log.i(DEBUG_TAG, "Main requestCode : " + requestCode + " resultCode :"
				+ resultCode);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case V.id.ADD_NOTE:
				Toast.makeText(this, "新记事已添加完成！", Toast.LENGTH_SHORT).show();
				break;
			// case V.id.FILTER_NOTE:
			// Toast.makeText(this, "筛选中...", Toast.LENGTH_SHORT)
			// .show();
			// break;
			default:
				break;
			}

		}

	}

}
